// pages/promoterManager/promoterManager.js
const userUtils = require("../../utils/user");
const validate = require('../../utils/validate');
const common = require('../../utils/common');
const wxp = require('../../utils/wxp.js');
const {
  getPromoterStats,
  getPromoterList
} = require('../../services/promoter');
const defaultPage = {
  curPage: 1,
  pageSize: 10,
  noMore: false
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    statsInfo: {},
    profitList: [{
      title: '今日收益',
      value: '0'
    }, {
      title: '预估收益',
      value: '0'
    }, {
      title: '当月收益',
      value: '0'
    }, {
      title: '累计收益',
      value: '0'
    }, {
      title: '当月销售',
      value: '0'
    }, {
      title: '总销售额',
      value: '0'
    }],
    query: {
      ...defaultPage
    },
    pInfo: {},
    teamList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({});
    wx.hideHomeButton({});
    // 初始化数据
    this.initData();
    // 
    this.onPullDownRefresh();
  },
  // 初始化数据
  initData() {
    this.setData({
      userInfo: userUtils.getUserInfo()
    });
  },

  queryChange: function (payload, pullDownRefresh) {
    this.setData({
      query: {
        ...this.data.query,
        ...payload
      }
    });
    this.getRemoteData(this.data.query, pullDownRefresh);
  },

  // 获取数据
  getRemoteData({
    curPage,
    pageSize
  }, pullDownRefresh) {
    var requestList = [];
    //我的团队数据
    let params = {
      curPage: curPage,
      pageSize: pageSize,
      shopId: this.data.userInfo.shopVo.id
    }
    requestList.push(getPromoterList(params));
    if (pullDownRefresh) {
      //业绩数据
      requestList.push(getPromoterStats());
    }
    //获取数据
    Promise.all(requestList).then(results => {
      //处理返回我的团队
      this.handlePromoterListData(results[0], curPage, pullDownRefresh);
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
        //处理返回推广团队业绩
        this.handlePromoterStats(results[1]);
      }
    }).catch(err => {
      if (pullDownRefresh) {
        wx.stopPullDownRefresh({});
      }
      common.log(err);
    });
  },

  // 处理首页数据
  handlePromoterListData(res, curPage, pullDownRefresh) {
    //设置数据
    var tmpList = this.data.teamList;
    if (pullDownRefresh) {
      tmpList = [];
    }
    //pageInfo
    var tmpPageInfo = this.data.query;
    if (res.page.pages <= curPage) {
      tmpPageInfo.noMore = true;
    } else {
      tmpPageInfo.curPage++;
    }
    //list
    if (res.data.length > 0) {
      tmpList = [...tmpList, ...res.data];
    }
    this.setData({
      query: tmpPageInfo,
      teamList: tmpList,
      pInfo: res.page
    });
  },

  // 处理推广团队业绩
  handlePromoterStats(res) {
    //今日收益
    let jrtjItem = this.data.profitList[0];
    jrtjItem.value = res.data.todayIncome;
    //预估收益
    let ygsyItem = this.data.profitList[1];
    ygsyItem.value = res.data.exceptedIncome;
    //当月收益
    let dysyItem = this.data.profitList[2];
    dysyItem.value = res.data.monthIncome;
    //累计收益
    let ljsyItem = this.data.profitList[3];
    ljsyItem.value = res.data.allIncome;
    //当月销售
    let dyxsItem = this.data.profitList[4];
    dyxsItem.value = res.data.monthSoldAmount;
    //总销售额
    let zxseItem = this.data.profitList[5];
    zxseItem.value = res.data.soldAmount;
    this.setData({
      profitList: this.data.profitList,
      statsInfo: res.data
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (payload, pullDownRefresh) {
    this.queryChange(defaultPage, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.query.noMore) return;
    this.queryChange(this.data.query);
  },

  // 查看明细
  onTeamDetail(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.teamList[index];
    let data = {
      realname: item.realname,
      shopId: item.shopId
    }
    wx.navigateTo({
      url: '../myPromoter/index?optionsData=' + common.jsonStringify(data),
    })
  },
  // 退出
  onLoginout() {
    userUtils.removeToken();
    userUtils.removeUserInfo();
    wx.redirectTo({
      url: '/pages/login/index'
    });
  }
})
